﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SLBehaviourSystem
{
    [CompositeNode]
    public class ParallelNode : CompositeNode
    {
        int completedCount = 0;

        protected override void OnInitData()
        {

        }

        protected override void OnExcute()
        {
            curNodeState = BehaviourNodeState.Running;
            for (int i = 0; i < childNodes.Count; i++)
            {
                curIndex = i;
                childNodes[i].Excute();
            }
        }

        protected override void OnReset()
        {
            base.OnReset();
            curIndex = 0;
            completedCount = 0;
        }

        protected override bool CheckInterruptState(BehaviourNodeState state)
        {
            return false;
        }

        protected override void OnChildNodeComplete()
        {
            completedCount = 0;
            for (int i = 0; i < childNodes.Count; i++)
            {
                if (childNodes[i].CurNodeState == BehaviourNodeState.Failure || childNodes[i].CurNodeState == BehaviourNodeState.Success)
                {
                    completedCount++;
                }
            }

            curNodeState = completedCount == childNodes.Count ? BehaviourNodeState.Success : BehaviourNodeState.Running;
            if (completedCount == childNodes.Count)
            {
                parent.ChildNodeComplete();
            }
        }
    }
}
